package com.ruoyi.utils;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dto.ReportDataDto;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author chenjc
 * @since 2021/03/24 17:31
 */
public class ReportUtil extends DateUtils {

    public static String firstMonth = "-01";

    public static String lastMonth = "-12";

    public static String[] monthName = new String[]{"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};

    public static Date getFirstDateTime(String year) {
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(DateUtils.dateTime(DateUtils.YYYY, year));
        tempCal.set(tempCal.get(1), 1, 1, 0, 0, 0);
        return new Timestamp(tempCal.getTime().getTime());
    }

    public static Date getLastDateTime(String year) {
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(DateUtils.dateTime(DateUtils.YYYY, year));
        tempCal.set(tempCal.get(1), 12, 31, 23, 59, 59);
        return new Timestamp(tempCal.getTime().getTime());
    }

    public static Timestamp getDayStart(Date date) {
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(date);
        tempCal.set(tempCal.get(1), tempCal.get(2), tempCal.get(5), 0, 0, 0);
        return new Timestamp(tempCal.getTime().getTime());
    }

    public static Timestamp getDayEnd(Date date) {
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(date);
        tempCal.set(tempCal.get(1), tempCal.get(2), tempCal.get(5), 23, 59, 59);
        return new Timestamp(tempCal.getTime().getTime());
    }

    public static ReportDataDto formatterLineData(List<ReportDataDto> reportDataDtos) {
        List<String> names = new ArrayList<>();
        List<String> moneys = new ArrayList<>();
        BigDecimal totalMoney = BigDecimal.ZERO;
        for (ReportDataDto item : reportDataDtos) {
            names.add(item.getName());
            BigDecimal money = new BigDecimal(item.getMoney());
            moneys.add(money.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            totalMoney = totalMoney.add(money);
        }
        ReportDataDto resultData = new ReportDataDto();
        resultData.setNames(names);
        resultData.setMoneys(moneys);
        resultData.setLength(reportDataDtos.size());
        resultData.setMoney(totalMoney.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        return resultData;
    }

    public static ReportDataDto formatterMonthData(List<ReportDataDto> reportDataDtos) {
        String[] moneys = new String[12];
        BigDecimal totalMoney = BigDecimal.ZERO;
        for (ReportDataDto item : reportDataDtos) {
            BigDecimal money = new BigDecimal(item.getMoney()).setScale(2, BigDecimal.ROUND_HALF_UP);
            moneys[Integer.parseInt(item.getName()) - 1] = money.toString();
            totalMoney = totalMoney.add(money);
        }
        ReportDataDto resultData = new ReportDataDto();
        resultData.setNames(Arrays.asList(monthName));
        resultData.setMoneys(Arrays.asList(moneys));
        resultData.setLength(reportDataDtos.size());
        resultData.setMoney(totalMoney.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        return resultData;
    }

    public static List<Map> formatterPieData(List<ReportDataDto> reportDataDtos) {
        List<Map> resultData = new ArrayList<>();
        for (ReportDataDto item : reportDataDtos) {
            Map<String, String> data = new HashMap<>();
            data.put("name", item.getName());
            data.put("value", new BigDecimal(item.getMoney()).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            resultData.add(data);
        }
        return resultData;
    }

    public static List<ReportDataDto> formatterWorkerData(List<ReportDataDto> reportDataDtos) {
        Map<String, List> formatterData = new HashMap<>();
        //根据师傅姓名组装信息
        for (ReportDataDto item : reportDataDtos) {
            String workerName = item.getTitle();
            if (!formatterData.containsKey(workerName)) {
                formatterData.put(workerName, new ArrayList());
            }
            formatterData.get(workerName).add(item);
        }
        //按月份排序组装数据
        List<ReportDataDto> resultData = new ArrayList<>();
        for (String key : formatterData.keySet()) {
            List<ReportDataDto> data = formatterData.get(key);
            String[] moneys = new String[12];
            for (ReportDataDto item : data) {
                BigDecimal money = new BigDecimal(item.getMoney()).setScale(2, BigDecimal.ROUND_HALF_UP);
                moneys[Integer.parseInt(item.getName()) - 1] = money.toString();
            }
            ReportDataDto reportDataDto = new ReportDataDto();
            reportDataDto.setTitle(key);
            reportDataDto.setNames(Arrays.asList(monthName));
            reportDataDto.setMoneys(Arrays.asList(moneys));
            resultData.add(reportDataDto);
        }
        return resultData;
    }

}
